/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Feb 22, 2012  7:45:25 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.service.promis.construction
 * File Name : SyncCostPropunitServices.java
 * TODO
 */
package jpa.service.promis.construction;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import org.apache.log4j.Logger;

import promis.utility.CommonService;
import jpa.entitys.promis.construction.SyncCostPropunit;
import jpa.service.common.JPAService;

/**
 * @author chatree
 *
 */
public class SyncCostPropunitServices extends JPAService {
	
	
	private static final Logger log = Logger.getLogger(SyncCostPropunitServices.class);

	/**
	 * @param em
	 */
	public SyncCostPropunitServices(EntityManager em) {
		super(em);
		// TODO Auto-generated constructor stub
	}

	public SyncCostPropunit findById(String id) throws Exception {
		
		return super.ConvertToYoureObject(super.find(id, "SyncCostPropunit.findById"));				
	}
	
	public List<SyncCostPropunit> findAll() throws Exception {
		return super.ConvertToYoureObject(super.findAll("SyncCostPropunit.findAll"));
	}
	
	public List<SyncCostPropunit> findByPlant(String buildingPhase, String sapPlantCode) throws Exception {
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("sapPlantCode", sapPlantCode);
		mapWhereValue.put("buildingPhase", buildingPhase);
		
		return super.ConvertToYoureObject(super.find("SyncCostPropunit.findByPlant", mapWhereValue));
	}
	
	public List<SyncCostPropunit> findByPlantStaging(String buildingPhase, String syncIsstaging, String sapPlantCode) throws Exception {
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("sapPlantCode", sapPlantCode);
		
		log.info("buildingPhase = " + buildingPhase);
		if(!CommonService.IsNull(buildingPhase) && !CommonService.IsNull(syncIsstaging)){
			mapWhereValue.put("syncIsstaging", syncIsstaging);
			mapWhereValue.put("buildingPhase", buildingPhase);
			
			return super.ConvertToYoureObject(super.find("SyncCostPropunit.findByPlantStaging", mapWhereValue));
			
		}else if(!CommonService.IsNull(syncIsstaging))
		{
			mapWhereValue.put("syncIsstaging", syncIsstaging);
			return super.ConvertToYoureObject(super.find("SyncCostPropunit.findByPlantStagingWithoutIsSync", mapWhereValue));
		}else{
			
			return super.ConvertToYoureObject(super.find("SyncCostPropunit.findByPlantStagingNoBul", mapWhereValue));
		}
	}
	
	public List<SyncCostPropunit> findBySyncIsStaging(String syncIsstaging) throws Exception {
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("syncIsstaging", syncIsstaging);
		
		return super.ConvertToYoureObject(super.find("SyncCostPropunit.findBySyncIsStaging", mapWhereValue));
	}
	
	public List<SyncCostPropunit> findByCompany(String syncIsstaging, String company) throws Exception {
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("syncIsstaging", syncIsstaging);
		mapWhereValue.put("company", company);
		
		return super.ConvertToYoureObject(super.find("SyncCostPropunit.findByCompany", mapWhereValue));
	}
	
	public List<SyncCostPropunit> findByProject(String syncIsstaging, String company, String project) throws Exception {
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("syncIsstaging", syncIsstaging);
		mapWhereValue.put("company", company);
		mapWhereValue.put("project", project);
		
		return super.ConvertToYoureObject(super.find("SyncCostPropunit.findByProject", mapWhereValue));
	}
	
	public List<SyncCostPropunit> findByBuilding(String syncIsstaging, String company, String project, String buildingPhase) throws Exception {
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("syncIsstaging", syncIsstaging);
		mapWhereValue.put("company", company);
		mapWhereValue.put("project", project);
		mapWhereValue.put("buildingPhase", buildingPhase);
		
		return super.ConvertToYoureObject(super.find("SyncCostPropunit.findByBuilding", mapWhereValue));
	}
	
	public List<SyncCostPropunit> findByZone(String syncIsstaging, String company, String project, String buildingPhase, String zoneCode) throws Exception {
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();		
		mapWhereValue.put("company", company);
		mapWhereValue.put("project", project);
		mapWhereValue.put("buildingPhase", buildingPhase);
		mapWhereValue.put("zoneCode", zoneCode);
		mapWhereValue.put("syncIsstaging", syncIsstaging);
		
		return super.ConvertToYoureObject(super.find("SyncCostPropunit.findByZone", mapWhereValue));
	}
	
	public SyncCostPropunit findByUnitNumber(String company, String project, String buildingPhase, String zoneCode, String unitNumber) throws Exception {
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("company", company);										//log.info("company = " + company);
		mapWhereValue.put("project", project);										//log.info("project = " + project);
		mapWhereValue.put("buildingPhase", buildingPhase);							//log.info("buildingPhase = " + buildingPhase);
		mapWhereValue.put("zoneCode", zoneCode);									//log.info("zoneCode = " + zoneCode);
		mapWhereValue.put("unitNumber", unitNumber);								//log.info("unitNumber = " + unitNumber);
		
		List<SyncCostPropunit> lst =  super.ConvertToYoureObject(super.find("SyncCostPropunit.findByUnitNumber", mapWhereValue));
		if(CommonService.getSize(lst) == 1)
			return lst.get(0);
		else
			return null;
		/*
		else
			throw new Exception("Data not Found !!!");
		 */
	}
	
	public List<SyncCostPropunit> findByUnitNumberByFloorZone(String company, String project, String buildingPhase, String floorZone) throws Exception {
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("company", company);										//log.info("company = " + company);
		mapWhereValue.put("project", project);										//log.info("project = " + project);
		mapWhereValue.put("buildingPhase", buildingPhase);							//log.info("buildingPhase = " + buildingPhase);
		mapWhereValue.put("zoneCode", floorZone);									//log.info("floorZone = " + floorZone);
		
		return super.ConvertToYoureObject(super.find("SyncCostPropunit.findByUnitNumber3", mapWhereValue));
	}
	
	public SyncCostPropunit findByUnitNumber(String company, String project, String buildingPhase, String zoneCode, String unitNumber, String syncIsstaging) throws Exception {
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("company", company);
		mapWhereValue.put("project", project);
		mapWhereValue.put("buildingPhase", buildingPhase);
		mapWhereValue.put("zoneCode", zoneCode);
		mapWhereValue.put("unitNumber", unitNumber);
		mapWhereValue.put("syncIsstaging", syncIsstaging);
		
		List<SyncCostPropunit> lst =  super.ConvertToYoureObject(super.find("SyncCostPropunit.findByUnitNumber1", mapWhereValue));
		if(CommonService.getSize(lst) == 1)
			return lst.get(0);
		else
			return null;
	}
	
	public List<SyncCostPropunit> findByUnitNumber(String company, String project, String buildingPhase) throws Exception {
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("company", company);										//log.info("company = " + company);
		mapWhereValue.put("project", project);										//log.info("project = " + project);
		mapWhereValue.put("buildingPhase", buildingPhase);							//log.info("buildingPhase = " + buildingPhase);		
		
		return super.ConvertToYoureObject(super.find("SyncCostPropunit.findByUnitNumber2", mapWhereValue));
		
	}

}
